package agentspeakeditor.editors;

import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
import org.eclipse.jface.text.DefaultInformationControl;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.reconciler.MonoReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.widgets.Shell;

import agentspeakeditor.editors.codeassist.ASLContentAssistant;
import agentspeakeditor.editors.common.scanner.ASLScanner;
import agentspeakeditor.editors.common.scanner.Scanner;
import agentspeakeditor.editors.folding.ASLReconcilingStrategy;


public class ASLConfiguration extends SourceViewerConfiguration {
	private Scanner scanner;
	private ASLScanner aslScanner;
	private ColorManager colorManager;
	private ASLContentAssistant contentAssistant = new ASLContentAssistant();
	private AgentSpeakEditor editor;

	public ASLConfiguration(ColorManager colorManager, AgentSpeakEditor editor) {
		this.colorManager = colorManager;
		this.editor = editor;
	}
	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[] {
			IDocument.DEFAULT_CONTENT_TYPE,
			ASLPartitionScanner.ASL_COMMENT,
			ASLPartitionScanner.ASL_PLAN,
			ASLPartitionScanner.ASL_DESIRE,
			ASLPartitionScanner.ASL_BELIEF,
			ASLPartitionScanner.ASL_CONTEXT};
	}

	protected Scanner getScanner() {
		if (scanner == null) {
			scanner = new Scanner(colorManager);
			scanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IASLColorConstants.DEFAULT))));
		}
		return scanner;
	}
	
	protected ASLScanner getASLScanner() {
		if (aslScanner == null) {
			aslScanner = new ASLScanner(colorManager);
			aslScanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IASLColorConstants.DEFAULT))));
		}
		return aslScanner;
	}

	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();

		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getScanner());
		reconciler.setDamager(dr, ASLPartitionScanner.ASL_PLAN);
		reconciler.setRepairer(dr, ASLPartitionScanner.ASL_PLAN);

		dr = new DefaultDamagerRepairer(getASLScanner());
		reconciler.setDamager(dr, ASLPartitionScanner.ASL_COMMENT);
		reconciler.setRepairer(dr, ASLPartitionScanner.ASL_COMMENT);
		
		dr = new DefaultDamagerRepairer(getScanner());
		reconciler.setDamager(dr, ASLPartitionScanner.ASL_DESIRE);
		reconciler.setRepairer(dr, ASLPartitionScanner.ASL_DESIRE);
		
		dr = new DefaultDamagerRepairer(getScanner());
		reconciler.setDamager(dr, ASLPartitionScanner.ASL_BELIEF);
		reconciler.setRepairer(dr, ASLPartitionScanner.ASL_BELIEF);
		
		dr = new DefaultDamagerRepairer(getASLScanner());
		reconciler.setDamager(dr,IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr,IDocument.DEFAULT_CONTENT_TYPE);

			return reconciler;
	}
	
	public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {				          
		 
		contentAssistant.enableAutoActivation(true);
		 
		contentAssistant.setAutoActivationDelay(500);
		contentAssistant.setProposalPopupOrientation(IContentAssistant.CONTEXT_INFO_BELOW);
		contentAssistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_BELOW);
				
		contentAssistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));

		return contentAssistant;
	}	
	
    /* (non-Javadoc)
     * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getReconciler(org.eclipse.jface.text.source.ISourceViewer)
     */
    public IReconciler getReconciler(ISourceViewer sourceViewer){
    	
    	ASLReconcilingStrategy strategy = new ASLReconcilingStrategy();
        strategy.setEditor(editor);
        
        MonoReconciler reconciler = new MonoReconciler(strategy,false);
        
        return reconciler;
    }
	
	public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
        return new IInformationControlCreator() {
            public IInformationControl createInformationControl(Shell parent) {
                return new DefaultInformationControl(parent,new HTMLTextPresenter(false));
            }
        };
    }
	

}